import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import { SimpleReport } from 'nc-report'
import { viewModel } from 'nc-lightapp-front';
import { get } from 'lodash-es'

const { getGlobalStorage } = viewModel;

export default class Ermloanbalance extends Component {
    constructor (props) {
        super(props)
        this.state = {}
    }

    disposeSearch (meta, props) {
        let DeptTreeOrgRef = 'nccloud.web.erm.action.ref.sqlbuilder.DeptTreeStopRef';//部门
        //获取收支项目参照
        let items = meta['light_report'].items
        items.forEach((item) => {
            let filter = item.referFilterAttr;
            if (filter) {
                if (filter[item.attrcode] && filter[item.attrcode].length > 0) {
                    filter[item.attrcode] = filter[item.attrcode].join(",");
                }
                else {
                    filter[item.attrcode] = '';
                }
            } else {
                filter = {};
                filter[item.attrcode] = '';
            }
            if (item.attrcode == 'deptid') {
                item.queryCondition = () => {
                    let data = props.search.getSearchValByField('light_report', 'pk_org').value.firstvalue;
                    return { pk_org: data,TreeRefActionExt:DeptTreeOrgRef,
                        DataPowerOperationCode: 'fi',
                        isDataPowerEnable: 'Y'}; // 根据pk_org过滤
                };
            }
            if (item.attrcode == 'jkbxr') {
                item.queryCondition = () => {
                    let pk_org = props.search.getSearchValByField('light_report', 'pk_org').value.firstvalue;
                    let deptid = props.search.getSearchValByField('light_report', 'deptid').value.firstvalue;
                    return {dept: deptid,pk_org: pk_org,
                        DataPowerOperationCode: 'fi',
                        isDataPowerEnable: 'Y'};
                };
            }
            if (item.attrcode === 'pk_org') {
                item.queryCondition = function () {
                    return {
                        TreeRefActionExt: 'nccloud.web.erm.action.ref.sqlbuilder.OrgTreeRefSqlBuilder',
                        DataPowerOperationCode: 'fi',
                        isDataPowerEnable: 'Y'
                    };
                };
            }
        })
        return meta; // 处理后的过滤参照返回给查询区模板
    }
    /**
     *
     * @param  items: 查询区查询数据，如需拓展查询区参数，请返回与items相同格式的查询数据
     */
    expandSearchVal (items) {
        return items
    }

    /**
      * searchId: 查询区需要的searchId参数
      * 'vname': 需要附默认值的字段
      * {value: '111'}: 显示值，区间为[]，具体可以对照平台查询区修改
      * 'like': 为oprtype字段值
    */
    setDefaultVal(searchId, props) {
        //查询区默认显示字段值
        //全部
        const conditionString = getGlobalStorage(
            'sessionStorage',
            `condition${props.getUrlParam('reportId')}*${props.getUrlParam('drillCode')}`
        );

        //全部
        if (conditionString) {
            try {
                const conditionObj = JSON.parse(conditionString);
                (conditionObj.conditions || []).forEach((item) => {
                    if (item.field === 'assume_org') {
                        item.field = 'pk_org';
                    }
                });
                props.search.setSearchValue(searchId, [...conditionObj.conditions]);
            } catch (error) {
                props.search.setSearchValue(searchId, []);
            }
            return;
        }

        props.search.setSearchValue(searchId, []);
    }

    /**
     * props: props
     * searchId: 查询区需要的searchId参数
     * field: 编辑后的key
     * val: 编辑后的value
    */
    onAfterEvent (props, searchId, field, val) {
    //查询区编辑后事件
        if (field === 'jkbxr') {
            const deptid = props.search.getSearchValByField(searchId, 'deptid');
            console.log(deptid);
            if (get(deptid, 'value.firstvalue') && get(val, 'values.pk_dept.value') !== get(deptid, 'value.firstvalue')) {
                props.search.setSearchValByField(searchId, 'deptid', {
                    value: get(val, 'values.pk_dept.value'),
                    display: get(val, 'values.deptname.value')
                });
            }
        } else if (field === 'deptid') {
            props.search.setSearchValByField(searchId, 'jkbxr', {
                value: null,
                display: null
            });
        }
    }

    /**
     * isRange: 查询后数据区域内点击单元格按钮可操作。如需要像升降序按钮那样，则设置 disabled={isRange}属性
     * data: 表格数据
     * coords: 选中的单元格
    */
    CreateNewSearchArea (isRange, data, coords) {
    // button区域业务端自定义的新增按钮
    //return <NCButton disabled={isRange} className="btn" shape="border">新增按钮</NCButton>
    }

    render() {
        return (
            <div className="table">
                <SimpleReport
                    showAdvBtn={true}
                    setDefaultVal={this.setDefaultVal.bind(this)}
                    expandSearchVal={this.expandSearchVal.bind(this)}
                    disposeSearch={this.disposeSearch.bind(this)}
                    onAfterEvent={this.onAfterEvent.bind(this)}
                    CreateNewSearchArea={this.CreateNewSearchArea.bind(this)}
                />
            </div>
        );
    }
}

ReactDOM.render(<Ermloanbalance/>,document.getElementById('app'));